﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TimbreRecognition.Util
{
    public class StatisticsUtil
    {
        public static Statictics GetStatistics(List<double[]> data)
        {
            double[] mean = GetMean(data);

            double spread = data.Sum(point => GetDistance(point, mean));

            spread = spread / data.Count;

            return new Statictics { Mean = mean, Spread = spread };
        }

        public static double GetDistance(double[] firstPoint, double[] secondPoint)
        {
            if (firstPoint.Length != secondPoint.Length)
            {
                throw new ArgumentException("Points must be of equal length");
            }

            double distance = firstPoint.Select((t, i) => Math.Pow(t - secondPoint[i], 2)).Sum();

            return Math.Sqrt(distance);
        }

        private static double[] GetMean(List<double[]> data)
        {
            double[] mean = new double[data[0].Length];

            foreach (double[] values in data)
            {
                for (int i = 0; i < values.Length; i++)
                {
                    mean[i] += values[i];
                }
            }

            return mean.Select(o => o/data.Count).ToArray();
        }
    }

    public class Statictics
    {
        public double[] Mean { get; set; }

        public double Spread { get; set; }
    }
}
